package com.etc.controller;

import com.etc.entity.Categorys;
import com.etc.entity.Movies;
import com.etc.service.MoviesServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/movies")
public class MoviesController {

    Logger logger = Logger.getLogger(MoviesController.class);

    @Autowired
    MoviesServiceImpl moviesService;
//    public String moviesDetail


    @RequestMapping("/selectMovies")
    @ResponseBody
    public PageInfo<Movies> selectMovies(@RequestParam(value = "movieName", defaultValue = "") String movieName,
                                         @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "5") int pageSize
    ) {
        PageInfo<Movies> pageInfo = moviesService.selectMovieByName2(movieName, pageNum, pageSize);

        logger.debug(pageInfo.getList().toString());

        return pageInfo;
    }

    /**
     * @author luliang
     * 查询首页top电影信息
     */
    @RequestMapping("/topImg")
    @ResponseBody
    public List<Movies> getTopImg(int startNo, int endNo) {
        List<Movies> firstImg = moviesService.getFirstImg(startNo, endNo);
        return firstImg;
    }

    /**
     * @author luliang
     */
    @RequestMapping("/getRandMovs")
    @ResponseBody
    public List<Movies> getRandMovs() {
        List<Movies> movs = moviesService.getRandMovies();
        return movs;
    }

    /**
     * @author luliang
     */
    @RequestMapping("/goOrderDtail")
    public ModelAndView goOrderDtail(@RequestParam(value = "movieNo") int movieNo) {
        ModelAndView mv = new ModelAndView();
        Movies mov = moviesService.getMovById(movieNo);
        mv.addObject("mov", mov);
        mv.setViewName("front/orderDtail");
        return mv;
    }

    /**
     * 后台添加电影
     */
    @RequestMapping("/addMovies")
    public String addMovies(@RequestParam(value = "movieName") String movieName,
                            @RequestParam(value = "movieContent") String movieContent,
                            @RequestParam(value = "movieImg") MultipartFile movieImg,
                            @RequestParam(value = "moviePrice") Double moviePrice,
                            @RequestParam(value = "movieDate") String movieDate,
                            @RequestParam(value = "movieTime") int movieTime,
                            @RequestParam(value = "cate") int categoryNo,
                            @RequestParam(value = "ctype") int typeNo) {
        String imgName = "无";
        logger.debug("第一次" + movieImg + ",");
        if (movieImg != null) {
            //2.获取原始的文件名
            imgName = movieImg.getOriginalFilename();
            //3.定义文件的存储位置
            File file = new File("E:\\uploadFileImg\\" + imgName);
            //4.写入
            try {
                movieImg.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logger.debug(imgName + ",");
        boolean i = moviesService.addMovie(movieName, movieContent, imgName, moviePrice, movieDate, movieTime, 1, categoryNo, typeNo);
        if (i) {
            return "back/moviesDetail";
        } else {
            return "back/addMovies";
        }
    }

    /**
     * 后台修改电影
     */
    @RequestMapping("/modifyMovie")
    public String modifyMovie(@RequestParam(value = "movieNo") int movieNo,
                              @RequestParam(value = "movieName") String movieName,
                              @RequestParam(value = "movieContent") String movieContent,
                              @RequestParam(value = "movieImg") MultipartFile movieImg,
                              @RequestParam(value = "moviePrice") Double moviePrice,
                              @RequestParam(value = "movieDate") String movieDate,
                              @RequestParam(value = "movieTime") int movieTime
                              ) {
       /* @RequestParam(value = "movieStatus") int movieStatus,
        @RequestParam(value = "categoryNo") int categoryNo,
        @RequestParam(value = "typeNo") int typeNo*/
        String imgName = "无";
        logger.debug("修改图片名1" + movieImg);
        if (movieImg != null) {
            //2.获取原始的文件名
            imgName = movieImg.getOriginalFilename();
            //3.定义文件的存储位置
            File file = new File("E:\\uploadFileImg\\" + imgName);
            //4.写入
            try {
                movieImg.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logger.debug("修改图片名2" + imgName);
        boolean i = moviesService.modifyMovie(movieName, movieContent, imgName, moviePrice, movieDate, movieTime, movieNo);
        if (i) {
            return "back/moviesDetail";
        } else {
            return "back/moviesDetail";
        }
    }

    /**
     * 电影添加页面
     */
    @RequestMapping("/movieAdd")
    @ResponseBody
    public String movieAdd() {
        return "addMovies";
    }

    /**
     * 删除电影
     */
    @RequestMapping("/deleteMovie")
    @ResponseBody
    public void deleteMovie(@RequestParam(value = "movieNo") int movieNo) {
        boolean i = moviesService.delMovieByNo(movieNo);
        logger.debug("删除" + i);
    }

    /**
     * updateMovieStatus
     * 恢复电信
     */
    @RequestMapping("/updateMovieStatus")
    @ResponseBody
    public void updateMovieStatus(@RequestParam(value = "movieNo") int movieNo) {
        boolean i = moviesService.updateMovieStatus(movieNo);
        logger.debug("恢复" + i);
    }


    //模糊查询所有电影
    @RequestMapping("/getAllMovies")
    @ResponseBody
    public PageInfo<Movies> getAllMovies(@RequestParam(value = "moviesName") String moviesName,
                                         @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        PageInfo<Movies> pageInfo = moviesService.selectMovieByName(moviesName, pageNum, pageSize);
        return pageInfo;
    }

    @RequestMapping("/getAllMov")
    @ResponseBody
    public List<Movies> getAllMov() {
        List<Movies> list = moviesService.selectMovieBy();
        return list;
    }

    @RequestMapping("/getAllcategorys")
    @ResponseBody
    public List<Categorys> getAllcategorys() {
        List<Categorys> list = moviesService.getMovieCategorys();
        return list;
    }
}
